from typing import List


class Solution:
    def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
        rowMax = [0] * len(grid)
        colMax = [0] * len(grid[0])
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                rowMax[i] = max(rowMax[i], grid[i][j])
                colMax[j] = max(colMax[j], grid[i][j])
        ans = 0
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                ans += min(rowMax[i], colMax[j]) - grid[i][j]
        return ans